package model;

import java.util.ArrayList;

/**
 * This class is responsible for performing search queries on Email objects.
 * 
 * This class contains a static method to search all emails of a user for 
 * the particular contents and returns the arraylists of email
 * @author buthainahaldosary
 * @since May 31, 2013
 * @version $Revision: 1.0 $
 */
 public class SearchEmail 
{
	 
	
	/**
	 * This method searches for all emails for a particular user that contain a 
	 * predefined text and returns an ArrayList of Email results.
	 * @param _text: Text to be searched for.
	 * @param _user: User to search emails of.
	 * @return Array lists of emails
	 */
	
	static ArrayList<Email> searchForEmails(String _text, User _user)
	{
		ArrayList<Email> result=new ArrayList<Email>();
		int folderSize;
		int emailSize;
		Email currentEmail;
		Boolean found;
		
		folderSize=_user.getFolders().size();
		
		for(int i=0;i<folderSize;i++)
		{
			emailSize=_user.getFolder(i).getEmails().size();
			
			for(int j=0;j<emailSize;j++)
			{
				currentEmail=_user.getFolder(i).getEmail(j);
				found=currentEmail.match(_text);		
				if(found=true)
					result.add(currentEmail);				
			}
		}	
		return result;
	}
		
}
